

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=auto>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.png">
  <link rel="icon" href="/img/favicon.png">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=5.0, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="author" content="Fly542">
  <meta name="keywords" content="">
  
    <meta name="description" content="显示当前的缓存状态show variables like &#39;%query_cache%&#39;;   显示当前的存储类型show engines;  删除数据库test_db下所有表select concat(&#39;drop table &#39;,table_name,&#39;;&#39;) from information_schema.tables where table_">
<meta property="og:type" content="article">
<meta property="og:title" content="mysql 常用命令">
<meta property="og:url" content="http://fly542.cn/2020/07/27/05%E5%88%86%E5%B8%83%E5%BC%8F%E5%8F%8A%E6%95%B0%E6%8D%AE%E5%BA%93/00%20%E4%BC%A0%E7%BB%9F%E6%95%B0%E6%8D%AE%E5%BA%93/00%20mysql/mysql%20%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/index.html">
<meta property="og:site_name" content="Fly542 技术沉淀">
<meta property="og:description" content="显示当前的缓存状态show variables like &#39;%query_cache%&#39;;   显示当前的存储类型show engines;  删除数据库test_db下所有表select concat(&#39;drop table &#39;,table_name,&#39;;&#39;) from information_schema.tables where table_">
<meta property="og:locale" content="zh_CN">
<meta property="article:published_time" content="2020-07-27T04:28:10.000Z">
<meta property="article:modified_time" content="2022-12-30T03:33:01.445Z">
<meta property="article:author" content="Fly542">
<meta property="article:tag" content="数据库">
<meta property="article:tag" content="mysql">
<meta name="twitter:card" content="summary_large_image">
  
  
  <title>mysql 常用命令 - Fly542 技术沉淀</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4/github-markdown.min.css" />
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/hint.css@2/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.css" />
  


<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_lbnruvf0jn.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->


  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"fly542.cn","root":"/","version":"1.8.14","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null,"path":"window.location.pathname","ignore_local":false}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 6.0.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand" href="/">
      <strong>Fly542 技术沉淀</strong>
    </a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/archives/">
                <i class="iconfont icon-archive-fill"></i>
                归档
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-category-fill"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" href="javascript:;" data-toggle="modal" data-target="#modalSearch" aria-label="Search">
              &nbsp;<i class="iconfont icon-search"></i>&nbsp;
            </a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self" href="javascript:;" aria-label="Color Toggle">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/default.png') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="mysql 常用命令">
              
            </span>

            
              <div class="mt-3">
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2020-07-27 12:28" pubdate>
        2020年7月27日 中午
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      2.1k 字
    </span>
  

  
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      18 分钟
    </span>
  

  
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">mysql 常用命令</h1>
            
            <div class="markdown-body">
              <ul>
<li><p>显示当前的缓存状态<br><code>show variables like &#39;%query_cache%&#39;; </code></p>
</li>
<li><p>显示当前的存储类型<br><code>show engines;</code></p>
</li>
<li><p>删除数据库test_db下所有表<br><code>select concat(&#39;drop table &#39;,table_name,&#39;;&#39;) from information_schema.tables where table_schema=&#39;test_db&#39;;</code></p>
</li>
<li><p>清空表数据<br>truncate tablename;</p>
</li>
<li><p>查询库csgw中所有表的记录数<br><code>use information_schema</code><br><code>select table_name,table_rows from tables where TABLE_SCHEMA=&#39;csgw&#39; order by table_rows desc;</code></p>
</li>
<li><p>显示当前数据库的user表结构<br>show columns from user;</p>
</li>
<li><p>切换当前数据库到whb_test<br><code>user whb_test;</code></p>
</li>
<li><p>创建一个user表，主键为name，20字符，passwd字段为64字符不能为空<br><code>CREATE TABLE user(name VARCHAR(20) not null primary key, passwd VARCHAR(64) not null, id int(4));</code></p>
</li>
<li><p>给一个用户赋予数据库权限</p>
<figure class="highlight sql"><table><tr><td class="gutter"><div class="code-wrapper"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></div></td><td class="code"><pre><code class="hljs sql">语法：mysql<span class="hljs-operator">&gt;</span> <span class="hljs-keyword">grant</span> 权限<span class="hljs-number">1</span>,权限<span class="hljs-number">2</span>,...权限n <span class="hljs-keyword">on</span> 数据库名称.表名称 <span class="hljs-keyword">to</span> 用户名@用户地址 identified <span class="hljs-keyword">by</span> <span class="hljs-string">&#x27;连接口令&#x27;</span>;<br>权限<span class="hljs-number">1</span>,权限<span class="hljs-number">2</span>,...权限n代表<br><span class="hljs-keyword">select</span>,<span class="hljs-keyword">insert</span>,update,<span class="hljs-keyword">delete</span>,<span class="hljs-keyword">create</span>,<span class="hljs-keyword">drop</span>,index,<span class="hljs-keyword">alter</span>,<span class="hljs-keyword">grant</span>,<span class="hljs-keyword">references</span>,reload,shutdown,process,file等<span class="hljs-number">14</span>个权限<br><br>实例：mysql<span class="hljs-operator">&gt;</span><span class="hljs-keyword">grant</span> <span class="hljs-keyword">select</span>,<span class="hljs-keyword">insert</span>,update,<span class="hljs-keyword">delete</span>,<span class="hljs-keyword">create</span>,<span class="hljs-keyword">drop</span> <span class="hljs-keyword">on</span> whb_test.employee <span class="hljs-keyword">to</span> joe<span class="hljs-variable">@10</span><span class="hljs-number">.163</span><span class="hljs-number">.225</span><span class="hljs-number">.87</span> identified <span class="hljs-keyword">by</span> <span class="hljs-string">&#x27;123456&#x27;</span>;<br>给来自<span class="hljs-number">10.163</span><span class="hljs-number">.225</span><span class="hljs-number">.87</span>的用户joe分配可对数据库whb_test的employee表进行<span class="hljs-keyword">select</span>,<span class="hljs-keyword">insert</span>,update,<span class="hljs-keyword">delete</span>,<span class="hljs-keyword">create</span>,<span class="hljs-keyword">drop</span>等操作的权限，并设定口令为<span class="hljs-number">123456</span>。<br>mysql<span class="hljs-operator">&gt;</span><span class="hljs-keyword">grant</span> <span class="hljs-keyword">all</span> privileges <span class="hljs-keyword">on</span> whb_test.<span class="hljs-operator">*</span> <span class="hljs-keyword">to</span> whb<span class="hljs-variable">@10</span><span class="hljs-number">.163</span><span class="hljs-number">.225</span><span class="hljs-number">.87</span> identified <span class="hljs-keyword">by</span> <span class="hljs-string">&#x27;123456&#x27;</span>;<br>给来自<span class="hljs-number">10.163</span><span class="hljs-number">.225</span><span class="hljs-number">.87</span>的用户whb分配可对数据库whb_test所有表进行所有操作的权限，并设定口令为<span class="hljs-number">123</span>。<br>mysql<span class="hljs-operator">&gt;</span><span class="hljs-keyword">grant</span> <span class="hljs-keyword">all</span> privileges <span class="hljs-keyword">on</span> <span class="hljs-operator">*</span>.<span class="hljs-operator">*</span> <span class="hljs-keyword">to</span> joe<span class="hljs-variable">@10</span><span class="hljs-number">.163</span><span class="hljs-number">.225</span><span class="hljs-number">.87</span> identified <span class="hljs-keyword">by</span> <span class="hljs-string">&#x27;123&#x27;</span>;<br>给来自<span class="hljs-number">10.163</span><span class="hljs-number">.225</span><span class="hljs-number">.87</span>的用户joe分配可对所有数据库的所有表进行所有操作的权限，并设定口令为<span class="hljs-number">123</span>。<br>mysql<span class="hljs-operator">&gt;</span><span class="hljs-keyword">grant</span> <span class="hljs-keyword">all</span> privileges <span class="hljs-keyword">on</span> <span class="hljs-operator">*</span>.<span class="hljs-operator">*</span> <span class="hljs-keyword">to</span> joe<span class="hljs-variable">@localhost</span> identified <span class="hljs-keyword">by</span> <span class="hljs-string">&#x27;123&#x27;</span>;<br>给本机用户joe分配可对所有数据库的所有表进行所有操作的权限，并设定口令为<span class="hljs-number">123</span>。<br><br>刷新权限：<br>FLUSH   PRIVILEGES;<br></code></pre></td></tr></table></figure></li>
<li><p>创建一个名为whb_test的数据库<br><code>CREATE DATABASE whb_test CHARACTER SET &#39;utf8&#39; COLLATE &#39;utf8_general_ci&#39;;</code></p>
</li>
<li><p>用whb用户登录172.16.1.110服务器的mysql：<br><code>         mysql -uwhb -p -h 172.16.1.110</code><br>使用此方法登录必须有ip</p>
</li>
<li><p>删除一个用户名为whb的用户</p>
<figure class="highlight pgsql"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs pgsql"><span class="hljs-keyword">drop</span> <span class="hljs-keyword">user</span> <span class="hljs-string">&#x27;whb&#x27;</span>@<span class="hljs-string">&#x27;%&#x27;</span>; <br>flush <span class="hljs-keyword">privileges</span>; <br></code></pre></td></tr></table></figure></li>
<li><p>创建一个用户名为whb，密码为123456的用户<br><code>create user whb identified by &#39;123456&#39;;</code></p>
</li>
<li><p> 创建一个密码为123456的用户test_user，对所有数据库有操作权限<br><code>grant select,insert,update,delete on *.* to test_user@&quot;%&quot; Identified by &quot;123456&quot;;</code></p>
</li>
<li><p>登录方法：<br><code>mysql -uxxxx -pxxxx --host=192.168.1.110  </code></p>
</li>
<li><p>root登录</p>
<figure class="highlight apache"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs apache"><span class="hljs-attribute">mysql</span> -uroot -p<span class="hljs-number">123456</span> mysql<br><span class="hljs-attribute">mysql</span> -utest_user -p<span class="hljs-number">123456</span> -h <span class="hljs-number">192.168.188.125</span> -P <span class="hljs-number">3306</span><br></code></pre></td></tr></table></figure></li>
<li><p>查看mytable表编码格式<br><code>show create table mytable; </code></p>
</li>
<li><p>查看test数据库的编码格式<br><code>show create database test;</code></p>
</li>
</ul>
<p>设置数据库db_name默认为utf8:<br>???</p>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a>
                    
                      <a class="hover-with-bg" href="/tags/mysql/">mysql</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2020/07/27/04DevOps/00%E5%B8%B8%E7%94%A8git%E5%91%BD%E4%BB%A4/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">常用git命令</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2020/07/27/01%E6%9E%B6%E6%9E%84%E8%AE%BE%E8%AE%A1/02UML%E7%B1%BB%E5%9B%BE%E5%9B%BE%E7%A4%BA%E6%A0%B7%E4%BE%8B/">
                        <span class="hidden-mobile">UML类图图示样例</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" aria-label="TOP" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  <script  src="/js/local-search.js" ></script>



  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  
    <script  src="https://cdn.jsdelivr.net/npm/tocbot@4/dist/tocbot.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3/dist/jquery.fancybox.min.js" ></script>
  
  
    <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4/anchor.min.js" ></script>
  
  
    <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2/dist/clipboard.min.js" ></script>
  






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
        typing(title);
      
    })(window, document);
  </script>















<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>


</body>
</html>
